package com.ruoyi.framework.youshuda.cleanup;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 应用关闭时，确保其他线程池正确关闭
 */
@Component
public class ThreadPoolShutdownHook implements DisposableBean {

    private final ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Override
    public void destroy() {
        executorService.shutdown(); // 停止接受新任务
        try {
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                executorService.shutdownNow(); // 强制关闭
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
        }
    }
}
